package com.wang.controller;


import com.wang.enums.BizCodeEnum;
import com.wang.request.AddressAddRequest;
import com.wang.service.AddressService;
import com.wang.util.JsonData;
import com.wang.vo.AddressVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 电商-公司收发货地址表 前端控制器
 * </p>
 *
 * @author 落日孤城
 * @since 2025-03-07
 */
@Api(tags = "收货地址模块")
@RestController
@RequestMapping("/api/address/v1")
public class AddressController {

    @Autowired
    private AddressService addressService;

    /**
     * 根据id获取地址详情
     * @param addressId
     * @return
     */
    @ApiOperation("根据id获取地址详情")
    @GetMapping("/find/{address_id}")
    public JsonData detail(@PathVariable("address_id") long addressId){
        AddressVO addressVO = addressService.detail(addressId);
        //可能存在用户输入的地址id不存在，所以要判断一下
        return addressVO != null ? JsonData.buildSuccess(addressVO) : JsonData.buildResult(BizCodeEnum.ADDRESS_NO_EXITS);
    }

    /**
     * 添加地址
     * @param addressAddRequest
     * @return
     */
    @ApiOperation("添加地址")
    @PostMapping("/add")
    public JsonData add(@RequestBody AddressAddRequest addressAddRequest){
        addressService.add(addressAddRequest);
        return JsonData.buildSuccess();
    }

    /**
     * 根据id删除地址
     * @param addressId
     * @return
     */
    @ApiOperation("根据id删除地址")
    @DeleteMapping("/del/{address_id}")
    public JsonData del(@PathVariable("address_id") long addressId){
        int rows = addressService.del(addressId);
        return rows == 1 ? JsonData.buildSuccess() : JsonData.buildResult(BizCodeEnum.ADDRESS_DEL_FAIL);
    }

    /**
     * 查询用户全部收货地址
     * @return
     */
    @ApiOperation("查询用户全部收货地址")
    @GetMapping("/findUserAllAddress")
    private JsonData findUserAllAddress(){//可以通过拦截器中的用户信息获取用户id，，所以不用传参
        List<AddressVO> list =  addressService.ListUserAllAddress();
        return JsonData.buildSuccess(list);
    }
}

